﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using FinWeb3.Models.Repository;

namespace FinWeb3.Models
{
    public class BankManager
    {
        private IRepository<Bank> repository;
        private IIdentity user;


        public BankManager(IRepository<Bank> repository, IIdentity user)
        {
            this.repository = repository;
            this.user = user;
        }

        public List<Bank> GetAll()
        {
            return 
                repository.GetAll(user)
                    .OrderBy(x => x.Name)
                    .ToList();
        }

        public Bank Get(int id)
        {
            return repository.Get(id, user);
        }

        public Bank Get(string bankId, string accountId)
        {
            return repository.GetAll(user).Where(x =>
                 x.BankId == bankId &&
                 x.AccountId == accountId &&
                 x.UserName == user.Name).FirstOrDefault();
        }

        public void Save(Bank bank)
        {
            repository.Save(bank, user);
        }
    }
}
